package cn.iocoder.yudao.module.alipay.controller.admin.babyordername;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.alipay.controller.app.babyordername.vo.*;
import cn.iocoder.yudao.module.alipay.convert.babyordername.BabyOrderNameConvert;
import cn.iocoder.yudao.module.alipay.dal.dataobject.babyordername.BabyOrderNameDO;
import cn.iocoder.yudao.module.alipay.service.babyordername.BabyOrderNameService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 宝宝起名订单名称关联")
@RestController
@RequestMapping("/alipay/baby-order-name")
@Validated
public class AppBabyOrderNameAdminController {

    @Resource
    private BabyOrderNameService babyOrderNameService;

    @PostMapping("/create")
    @Operation(summary = "创建宝宝起名订单名称关联")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:create')")
    public CommonResult<Long> createBabyOrderName(@Valid @RequestBody AppBabyOrderNameCreateReqVO createReqVO) {
        return success(babyOrderNameService.createBabyOrderName(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新宝宝起名订单名称关联")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:update')")
    public CommonResult<Boolean> updateBabyOrderName(@Valid @RequestBody AppBabyOrderNameUpdateReqVO updateReqVO) {
        babyOrderNameService.updateBabyOrderName(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除宝宝起名订单名称关联")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:delete')")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBabyOrderName(@RequestParam("id") Long id) {
        babyOrderNameService.deleteBabyOrderName(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得宝宝起名订单名称关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:query')")
    public CommonResult<AppBabyOrderNameRespVO> getBabyOrderName(@RequestParam("id") Long id) {
        BabyOrderNameDO babyOrderName = babyOrderNameService.getBabyOrderName(id);
        return success(BabyOrderNameConvert.INSTANCE.convert(babyOrderName));
    }

    @GetMapping("/getByTradeNo")
    @Operation(summary = "根据订单号获得宝宝起名订单名称关联")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:query')")
    public CommonResult<AppBabyOrderNameRespVO> getByTradeNo(String tradeNo) {
        BabyOrderNameDO babyOrderName = babyOrderNameService.getByTradeNo(tradeNo);
        return success(BabyOrderNameConvert.INSTANCE.convert(babyOrderName));
    }

    @GetMapping("/list")
    @Operation(summary = "获得宝宝起名订单名称关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:query')")
    public CommonResult<List<AppBabyOrderNameRespVO>> getBabyOrderNameList(@RequestParam("ids") Collection<Long> ids) {
        List<BabyOrderNameDO> list = babyOrderNameService.getBabyOrderNameList(ids);
        return success(BabyOrderNameConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得宝宝起名订单名称关联分页")
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:query')")
    public CommonResult<PageResult<AppBabyOrderNameRespVO>> getBabyOrderNamePage(@Valid AppBabyOrderNamePageReqVO pageVO) {
        PageResult<BabyOrderNameDO> pageResult = babyOrderNameService.getBabyOrderNamePage(pageVO);
        return success(BabyOrderNameConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出宝宝起名订单名称关联 Excel")
    @OperateLog(type = EXPORT)
    @PreAuthorize("@ss.hasPermission('alipay:baby-order-name:export')")
    public void exportBabyOrderNameExcel(@Valid AppBabyOrderNameExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BabyOrderNameDO> list = babyOrderNameService.getBabyOrderNameList(exportReqVO);
        // 导出 Excel
        List<AppBabyOrderNameExcelVO> datas = BabyOrderNameConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "宝宝起名订单名称关联.xls", "数据", AppBabyOrderNameExcelVO.class, datas);
    }

}
